home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / egs-tools / egs_dev-disk / egsdemos / moreexamples / menus / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  8.1 KB  |  262 lines

  1.  
  2. #include <stdio.h>
  3. #include "/global/userwindow.h"
  4. #include "/global/egslibraries.h"
  5.  
  6. #include <egs/egsintui.h>
  7. #include <egs/clib/egsintui_protos.h>
  8. #include <egs/pragmas/egsintui_pragmas.h>
  9. #include <egs/egsgadbox.h>
  10. #include <egs/clib/egsgadbox_protos.h>
  11. #include <egs/pragmas/egsgadbox_pragmas.h>
  12. #include <egs/egsgfx.h>
  13. #include <egs/clib/egsgfx_protos.h>
  14. #include <egs/pragmas/egsgfx_pragmas.h>
  15.  
  16.  
  17. #define TESTING_ID 200
  18.  
  19. #define PROJECT_BASE_ID 500
  20. #define EXCLUDE_BASE_ID 600
  21.  
  22. struct UserInputWindow Example_Request;
  23. EG_EFontPtr MenuFont=NULL;     // Menu Font.
  24. EI_MenuPtr ExampleMenus=NULL;   // Menu Pointer.
  25.  
  26. BYTE Open_Menus (void);
  27. void Close_Menus (void);
  28. EI_MenuPtr InitExampleMenus (EG_EFontPtr font);
  29. EB_GadBoxPtr Create_Example (UserInputWindowPtr UserWindow);
  30. void GadgetDown_Example (UserInputWindowPtr UserWindow, EI_GadgetPtr iaddress);
  31. void Menu_Example (UserInputWindowPtr UserWindow, UWORD code);
  32.  
  33. void ToggleGhost_Menu_Item (long code);
  34. void UnGhost_Menu_Item (long code);
  35. void Ghost_Menu_Item (long code);
  36.  
  37.  
  38. void main (void) {
  39.  
  40.   if (OpenEGSLibraries ()) {
  41.     if (Open_Menus ()) {
  42.       Init_UserWindow (&Example_Request, "Example Window",WINDOW_MODAL,ExampleMenus,
  43.                        NULL);
  44.       Example_Request.Create     = Create_Example;
  45.       Example_Request.GadgetDown = GadgetDown_Example;
  46.       Example_Request.Menu       = Menu_Example;
  47.  
  48.       Example_Request.IDCMPFlags = EI_iCLOSEWINDOW | EI_iSIZEVERIFY |
  49.                                    EI_iNEWSIZE | EI_iGADGETDOWN |
  50.                                    EI_iMENUPICK;
  51.       Example_Request.Flags =      EI_SIZEBBOTTOM| EI_SIMPLE_REFRESH |
  52.                                    EI_GIMMEZEROZERO ;
  53.       Example_Request.SysGadgets = (EI_WINDOWCLOSE)|(EI_WINDOWSIZE)|EI_WINDOWDRAG;
  54.  
  55.       if (Open_UserWindow (&Example_Request,-1,-1)) {
  56.         Close_UserWindow (&Example_Request);
  57.       }
  58.       else {
  59.         printf ("Couldn't open my window!\n");
  60.       }
  61.       Close_Menus ();
  62.     }
  63.     else {
  64.       printf ("Couldn't open Menus for Example.\n");
  65.     }
  66.   }
  67.   CloseEGSLibraries ();  // after the bracket cause what if I opened 3 but
  68.                          // couldn't find the rest, still need to close the 3.
  69. }
  70.  
  71. /*******************************
  72.              PUBLIC:       Open_Menus.
  73.                            This routine opens our menus.
  74. *******************************/
  75. BYTE Open_Menus (void) {
  76.   ExampleMenus=NULL;
  77.   if (!(ExampleMenus= InitExampleMenus (MenuFont))) {
  78.     printf ("Could not Initialize Menus!\n");
  79.     return (0);
  80.   }
  81.   return (1);
  82. }
  83.  
  84.  
  85. /******************************
  86.            PUBLIC:      Close_Menus.
  87.                         This routine closes the menus.
  88. *******************************/
  89. void Close_Menus (void) {
  90.  
  91.   if (ExampleMenus) {
  92.     EI_FreeMenu (ExampleMenus);
  93.     ExampleMenus=NULL;
  94.   }
  95.   if (MenuFont) {
  96.     EG_CloseFont (MenuFont);
  97.     MenuFont=NULL;
  98.   }
  99. }
  100.  
  101.  
  102. EI_MenuPtr InitExampleMenus (EG_EFontPtr font) {
  103.   EI_MenuPtr menu;
  104.   EI_MenuItemPtr item1, tempitem;
  105.   struct TextAttr *attr;
  106.  
  107.   if (font==NULL) {
  108.     attr = (struct TextAttr *) EI_GetPrefFont (1);
  109.     font = (EG_EFontPtr)      EG_OpenFont (attr);
  110.   }
  111.  
  112.   if (menu=EI_CreateMenu ()) {
  113.       item1 = EI_CreateItem (font,"Project",PROJECT_BASE_ID,0,0);
  114.       EI_AddToItem (item1,EI_CreateLeave ());
  115.       EI_AddToItem (item1,EI_CreateItem (font,"Open...",PROJECT_BASE_ID+1,0,0));
  116.       EI_AddToItem (item1,EI_CreateItem (font,"Save",PROJECT_BASE_ID+2,0,0));
  117.       EI_AddToItem (item1,EI_CreateItem (font,"Save as...",PROJECT_BASE_ID+3,0,0));
  118.       EI_AddToItem (item1,EI_CreateItem (font,"Quit",PROJECT_BASE_ID+4,0,0));
  119.     EI_AddToMenu (menu,item1);
  120.                                  // These menu items are mutually excluded.
  121.                                  // They also have keyboard equivs. (1,2,3,4)
  122.                                  // NOTE! if we didn't have a CreateLeave in
  123.                                  // this menu, we would start with (1<<0),
  124.                                  // not (1<<1). Wierd but true!
  125.       item1 = EI_CreateItem (font,"Excludes",EXCLUDE_BASE_ID,0,0);
  126.       EI_AddToItem (item1,EI_CreateLeave ());
  127.       tempitem=EI_CreateItem (font,"EXCLUDE 1",EXCLUDE_BASE_ID+1,'1',1);
  128.       tempitem->MutualExclude = ~(1<<1);
  129.       tempitem->Flags &= ~(EI_MENU_TOGGLE);
  130.       EI_AddToItem (item1,tempitem);
  131.       tempitem=EI_CreateItem (font,"EXCLUDE 2",EXCLUDE_BASE_ID+2,'2',1);
  132.       tempitem->MutualExclude = ~(1<<2);
  133.       tempitem->Flags &= ~(EI_MENU_TOGGLE);
  134.       EI_AddToItem (item1,tempitem);
  135.       tempitem=EI_CreateItem (font,"EXCLUDE 3",EXCLUDE_BASE_ID+3,'3',1);
  136.       tempitem->MutualExclude = ~(1<<3);
  137.       tempitem->Flags &= ~(EI_MENU_TOGGLE);
  138.       EI_AddToItem (item1,tempitem);
  139.       tempitem=EI_CreateItem (font,"EXCLUDE 4",EXCLUDE_BASE_ID+4,'4',1);
  140.       tempitem->MutualExclude = ~(1<<4);
  141.       tempitem->Flags &= ~(EI_MENU_TOGGLE);
  142.       EI_AddToItem (item1,tempitem);
  143.     EI_AddToMenu (menu,item1);
  144.     return menu;
  145.   }
  146.   else {
  147.     printf ("can't EI_CreateMenu!\n");
  148.     return NULL;
  149.   }
  150. }
  151.  
  152. /*************************************
  153.    PRIVATE:  Create_Example.
  154.              This routine is called by the UserWindow routines.
  155.              It passes back a GadBoxPtr to the gadgets wanted.
  156. *************************************/
  157. EB_GadBoxPtr Create_Example (UserInputWindowPtr UserWindow) {
  158.   EB_GadBoxPtr root,help;
  159.   EB_GadContext gadcon;
  160.  
  161.   gadcon = UserWindow->GadCon;
  162.   root = EB_CreateVertiBox (gadcon);
  163.   EB_AddLastSon (root,EB_CreateVertiFill (gadcon,0,0));
  164.   EB_AddLastSon (root,EB_CreateText (gadcon,"Hit the right mouse button."));
  165.   EB_AddLastSon (root,EB_CreateText (gadcon,"This example has menus!"));
  166.   EB_AddLastSon (root,EB_CreateText (gadcon,"Keyboard equivs work with the right amiga key."));
  167.   help=EB_CreateTextAction (gadcon,"Ghost Save as Menu Item",TESTING_ID,EB_FILL_ALL);
  168.   EB_NewPri (help,-1);
  169.   EB_AddLastSon (root,help);
  170.  
  171.  
  172.   EB_AddLastSon (root,EB_CreateVertiFill (gadcon,0,0));
  173.   return root;
  174. }
  175.  
  176. /**************************************
  177.     PRIVATE:  GadgetDown_Example.
  178.               This routine handles the gadget down events passed to the
  179.               Example Control Window.
  180. **************************************/
  181. void GadgetDown_Example (UserInputWindowPtr UserWindow, EI_GadgetPtr iaddress) {
  182.  
  183.  
  184.   switch (iaddress->GadgetID) {
  185.     case TESTING_ID:
  186.       printf ("Toggling ghosting on Menu item 'Save as'\n");
  187.       ToggleGhost_Menu_Item (PROJECT_BASE_ID+3);
  188.     break;
  189.   }
  190. }
  191. /**************************************
  192.     PRIVATE:  Menu_Example.
  193.               This routine handles the Menu events passed to the
  194.               Example Control Window.
  195. **************************************/
  196. void Menu_Example (UserInputWindowPtr UserWindow, UWORD code) {
  197.  
  198.   switch (code) {
  199.     case PROJECT_BASE_ID+1:
  200.       printf ("Project: Open\n");
  201.     break;
  202.     case PROJECT_BASE_ID+2:
  203.       printf ("Project: Save\n");
  204.     break;
  205.     case PROJECT_BASE_ID+3:
  206.       printf ("Project: Save as...\n");
  207.     break;
  208.                             // Here is how to signal a MODAL window to
  209.                             // close early.
  210.     case PROJECT_BASE_ID+4:
  211.       printf ("Project: Quit.\n");
  212.       UserWindow->Signals |= SIGNAL_CLOSE;
  213.     break;
  214.   }
  215. }
  216.  
  217. /***************************
  218.         Feel free to use these routines in your code. They handle ghosting
  219.         a Menu item for you.
  220. **************************/
  221.  
  222. void Ghost_Menu_Item (long code) {
  223.   EI_MenuItemPtr item;
  224.   EI_MenuPtr menu;
  225.  
  226.   if (menu = EI_FindSubMenu (ExampleMenus,code)) {
  227.     if (item = EI_FindMenuItem (menu,code)) {
  228.       if (item->Flags&(EI_MENU_ACTIVE)) {
  229.         EI_OffMenuItem (menu,item);
  230.       }
  231.     }
  232.   }
  233. }
  234.  
  235. void UnGhost_Menu_Item (long code) {
  236.   EI_MenuItemPtr item;
  237.   EI_MenuPtr menu;
  238.  
  239.   if (menu = EI_FindSubMenu (ExampleMenus,code)) {
  240.     if (item = EI_FindMenuItem (menu,code)) {
  241.       if (!(item->Flags&(EI_MENU_ACTIVE)) ) {
  242.         EI_OnMenuItem (menu,item);
  243.       }
  244.     }
  245.   }
  246. }
  247.  
  248. void ToggleGhost_Menu_Item (long code) {
  249.   EI_MenuItemPtr item;
  250.   EI_MenuPtr menu;
  251.  
  252.   if (menu = EI_FindSubMenu (ExampleMenus,code)) {
  253.     if (item = EI_FindMenuItem (menu,code)) {
  254.       if (!(item->Flags&(EI_MENU_ACTIVE)) ) {
  255.         EI_OnMenuItem (menu,item);
  256.       }
  257.       else {
  258.         EI_OffMenuItem (menu,item);
  259.       }
  260.     }
  261.   }
  262. }